<?php
namespace app\admin\controller;
use think\View;

class Links extends Auth
{
	public function __construct()
	{
	    parent::__construct();
        $this->isLogin();
		$this->Links = D('links');
        $active['function']['links']=1;
        $this->assign('active', $active);
	}
	
	public function index($lid=0)
	{   
	
	   //test
     
	   //test END
	

	
	
	
	    //类别筛选
		if(IS_POST){
	    $lid = $_POST['lid'];
		}
		$links = $this->Links->getLinks($lid);
		$count= count($links);
		$Page = $this->getPage($count,C('PAGE_NUMS'));
		if($lid == 0){
			$get_links=$this->Links->limit($Page->firstRow.','.$Page->listRows)->select();
		}else{
			$get_links=$this->Links->where('lid='.$lid)->limit($Page->firstRow.','.$Page->listRows)->select();
		}
		
		
		
		$this->view->assign('get_links',$get_links);
		
		//链接类别
		$link_type = M('linktype')->select();
		$this->view->assign('link_type',$link_type);

		return $this->view->fetch();
	}
	
	public function add()
	{

		$linktype = $this->Links->selectLinksType();
		$this->view->assign('linktype',$linktype);
		
		if(IS_POST){
		  $add_links = $this->Links->addLinks();
		    if($add_links){
			   manage_log_success();
			  return $this->success('发布成功', U('Links/index'));
		   }else{
			    manage_log_error();
			  return $this->error('发布失败', U('Links/index'));
		   }	
		}
		return $this->view->fetch();
	}
	
	public function del($id)
	{
	  $reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm){
			 return $this->error('取消删除', U('Links/index'));
		}
		$del_links = $this->Links->delLinks($id);
		 if($del_links){
			 manage_log_success();
			return $this->success('删除成功', U('Links/index'));
		 }else{
			 manage_log_error();
			return $this->error('删除失败', U('Links/index'));
		 }
	}
	
	public function edit($id)
	{
		$edit = $this->Links->getInfo($id);
		$this->view->assign('edit',$edit);
		return $this->view->fetch();
	}
	
	public function  postEdit()
	{
		$post_edit = $this->Links->postEdit();
		 if($post_edit !==false){
			 manage_log_success();
			return $this->success('修改成功', U('Links/index'));
		 }else{
			 manage_log_error();
			return $this->error('修改失败', U('Links/index'));
		 }
	}
	
	//添加linktype,考虑简单就直接不写新的模型
	public function lid_index()
	{
		$linktype = $this->Links->linkType();
		$this->view->assign('linktype',$linktype);
		return $this->view->fetch();
	}
	
	public function lid_add()
	{
		if(IS_POST){
			$lid = $this->Links->lid();
			 if($lid){
				 manage_log_success();
			    return $this->success('添加成功', U('Links/lid_index'));
		    }else{
				 manage_log_error();
			    return $this->error('添加失败', U('Links/lid_index'));
		   }
		}
		return $this->view->fetch();
		
	}
	
	public function lid_del($id){
		$del = $this->Links->lidDel($id);
		 if($del){
			  manage_log_success();
			return $this->success('删除成功', U('Links/lid_index'));
		 }else{
			 manage_log_error();
			return $this->error('删除失败', U('Links/lid_index'));
		 }
	}
	
	public function lid_edit($id)
	{
		
		$edit = $this->Links->getLidInfo($id);
		$this->view->assign('edit',$edit);
		return $this->view->fetch();
	}
	public function postLidEdit($id)
	{
		$edit = $this->Links->postLidEdit($id);
		 if($edit !==false){
			 manage_log_success();
			return $this->success('修改成功', U('Links/lid_index'));
		 }else{
			 manage_log_error();
			return $this->error('修改失败', U('Links/lid_index'));
		 }
	}
	
		public function listorder($listorder,$listorder_id)
	{
		$list_order = $this->Links->listOrder($listorder,$listorder_id);
		 if($list_order){
			  manage_log_success();
		 }else{
			  manage_log_error();
		 }
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
